package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.Chance;
import com.sl.au.entity.ChanceTrance;
import com.sl.au.entity.User;
import com.sl.au.service.ChanceService;
import com.sl.au.service.ChanceTranceService;
import com.sl.au.service.OperateLogService;

@RestController
@RequestMapping(value = "/ui/chanceTrance")
public class ChanceTranceController extends BaseController {
	
	@Autowired
	ChanceTranceService chanceTranceService;
	
	@Autowired
	ChanceService chanceService;
	
	@Autowired
	OperateLogService operateLogService;
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("chanceTrance/chanceTranceindex");
		Account a = this.getMyAccount();
		String userId =a.getUser().getId();
		modelAndView.addObject("userId",userId);
		return modelAndView;
	}
	/**
	 * 信息跟踪弹窗保存
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Map<String, Object> saveCoopSaleChance(HttpServletRequest request, @RequestBody ChanceTrance chanceTrance) {
		Map<String, Object> map = new HashMap<String, Object>();
		String uuid = java.util.UUID.randomUUID().toString();
		String id = chanceTrance.getId();
		if(!StringUtils.hasText(id)){
			chanceTrance.setId(uuid);
		}
		chanceTranceService.getRepository().save(chanceTrance);
		//增加操作日志
		Account a = this.getMyAccount();
		User user = a.getUser();
		Chance chance = chanceService.getRepository().findOne(chanceTrance.getSaleChance().getId());
		Integer d = chanceTrance.getOrderAmount();
		if(d!=null && d>0){
			chance.setOrderAmount(d);
			chanceService.getRepository().save(chance);
		}
		String content = "跟踪销售信息【"+chance.getName()+"】";
		operateLogService.log(user, "信息跟踪", content);
		map.put("code", 0);
		return map;
	}
	/**
	 * 分配员工跟踪情况数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getMyTrance", method = RequestMethod.GET)
	public Map<String,Object> getMyTrance(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,String> condition = new HashMap<String,String>();
		Account a = this.getMyAccount();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String chanceId = request.getParameter("chanceId");
		String userId = a.getUser().getId();
		List<ChanceTrance> users = null;
        condition.put("chanceId", chanceId);
        condition.put("userId", userId);
		users = chanceTranceService.getMyPage(start, length, condition, null, null);
        int count = chanceTranceService.getRepository().findMyTranceCount(chanceId, userId);
		
		map.put("data", users);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	/**
	 * 所有员工跟踪情况数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getAllTrance", method = RequestMethod.GET)
	public Map<String,Object> getAllTrance(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		Account a = this.getMyAccount();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		List<ChanceTrance> users = null;
		String chanceId =request.getParameter("chanceId");
		users = chanceTranceService.getAllTrance(start, length, chanceId, null, null);
        long count = chanceTranceService.getRepository().findAllTranceCount(chanceId);
		
		map.put("data", users);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	//删除跟踪情况
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public Map<String, Object> delBid(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
	    String ids=request.getParameter("ids");
	    String id[]=ids.split(",");
	    List<ChanceTrance> ChanceTrances=new ArrayList<ChanceTrance>();
	    for(int i=0;i<id.length;i++){
	    	String tranceId = id[i];
	    	ChanceTrance chanceTrance=new ChanceTrance();
	    	chanceTrance.setId(tranceId);
	    	ChanceTrances.add(chanceTrance);
	    }
	    chanceTranceService.getRepository().deleteInBatch(ChanceTrances);
		map.put("code", 0);
		return map;	
	}
	//删除跟踪情况mobile
		@RequestMapping(value="{id}",method = RequestMethod.DELETE)
		public Map<String, Object> delTrack(@PathVariable String id){
			Map<String, Object> map = new HashMap<String, Object>();		  
		    chanceTranceService.getRepository().delete(id);
			map.put("code", 0);
			return map;	
		}
}
